package a07_二叉树;

/**
 * <p>
 * a23_验证二叉搜索树复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/2/24
 */
public class a23_验证二叉搜索树复习2 {

    public static void main(String[] args) {
        a23_验证二叉搜索树复习2 test = new a23_验证二叉搜索树复习2();
        TreeNode treeNode = new TreeNode(5,
                new TreeNode(1),
                new TreeNode(4, new TreeNode(3), new TreeNode(6)));
        System.out.println(test.isValidBST(treeNode));

    }

    private TreeNode prevNode;

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        boolean leftValidBST = isValidBST(root.left);

        if (prevNode != null) {
            if(root.val <= prevNode.val){
                return false;
            }

        }
        prevNode = root;

        boolean rightValidBST = isValidBST(root.right);
        return leftValidBST && rightValidBST;
    }
}
